
package org.wk.upms.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wk.core.common.view.PageVo;
import org.wk.core.common.view.ResponseVo;
import org.wk.logger.common.annotation.SysLog;
import org.wk.upms.service.RoleDeptService;
import org.wk.upms.service.RoleResourceService;
import org.wk.upms.service.RoleService;
import org.wk.upms.view.RoleVo;

import java.util.List;
import java.util.Map;

/**
 * 角色管理
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {
	@Autowired
	private RoleService sysRoleService;
	@Autowired
	private RoleResourceService sysRoleMenuService;
	@Autowired
	private RoleDeptService sysRoleDeptService;
	
	/**
	 * 角色列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:role:list")
	public ResponseVo list(@RequestParam Map<String, Object> params){
		PageVo page = sysRoleService.queryPage(params);

		return ResponseVo.success().setData(page);
	}
	
	/**
	 * 角色列表
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:role:select")
	public ResponseVo select(){
		List<RoleVo> list = sysRoleService.selectList(null);
		
		return ResponseVo.success().setData (list);
	}
	
	/**
	 * 角色信息
	 */
	@RequestMapping("/info/{roleId}")
	@RequiresPermissions("sys:role:info")
	public ResponseVo info(@PathVariable("roleId") String roleId){
		RoleVo role = sysRoleService.selectById(roleId);
		
		//查询角色对应的功能权限
		List<String> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIds(menuIdList);

		//查询角色对应的数据权限
		List<String> deptIdList = sysRoleDeptService.queryDeptIdList(roleId);
		role.setDeptIds(deptIdList);
		
		return ResponseVo.success().setData (role);
	}
	
	/**
	 * 保存角色
	 */
	@SysLog("保存角色")
	@RequestMapping("/save")
	@RequiresPermissions("sys:role:save")
	public ResponseVo save(@RequestBody RoleVo role){
//		ValidatorUtils.validateEntity(role);
		
		sysRoleService.save(role);
		
		return ResponseVo.success();
	}
	
	/**
	 * 修改角色
	 */
	@SysLog("修改角色")
	@RequestMapping("/update")
	@RequiresPermissions("sys:role:update")
	public ResponseVo update(@RequestBody RoleVo role){
//		ValidatorUtils.validateEntity(role);
		
		sysRoleService.update(role);
		
		return ResponseVo.success();
	}
	
	/**
	 * 删除角色
	 */
	@SysLog("删除角色")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:role:delete")
	public ResponseVo delete(@RequestBody String[] roleIds){
		sysRoleService.deleteBatch(roleIds);
		
		return ResponseVo.success();
	}
}
